package com.liu.HomeWork;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;

/**
 *
 * 客户端
 */
public class HomeWork03Client {
    public static void main(String[] args) throws Exception {
        //客户端连接服务端
        Socket socket = new Socket(InetAddress.getByName("192.168.56.1"), 4561);
        //接收用户输入，指定下载名
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你需要的音乐名称");
        String downLoadFileName = scanner.nextLine();
        //获取和Socket相关的输出流
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write(downLoadFileName.getBytes());
        //设置写入结束标志
        socket.shutdownOutput();
        //读取服务端返回的文件
        BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
        byte[] bytes = StreamUtils.streamToByteArray(bis);
        //得到一个输出流，将bytes写入到磁盘文件
        String filePath = "d:\\CloudMusic\\" + downLoadFileName + ".mp3";
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
        bos.write(bytes);
        //关闭流
        bis.close();
        bos.close();
        socket.close();
        outputStream.close();
        System.out.println("客户端下载完毕~");
    }
}
